<template>
    <div >
        <el-button type="primary" @click="handleAddLocation">新增区域</el-button>
        <el-table :Data="locationList" border style="width: 100%">
            <el-table-column prop="Id" label="区域ID" width="180"></el-table-column>
            <el-table-column prop="Name" label="区域名称"></el-table-column>
            <el-table-column prop="Area" label="所属大区"></el-table-column>
            <el-table-column label="操作" width="180">
                <template #default="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="handleDelete(scope.row.Id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog v-model="dialogVisible" :title="dialogTitle" width="50%">
            <el-form :model="currentLocation" label-width="120px">
                <el-form-item label="区域名称">
                    <el-input v-model="currentLocation.Name"></el-input>
                </el-form-item>
                <el-form-item label="所属大区">
                    <el-select v-model="currentLocation.Area" placeholder="请选择大区">
                        <el-option label="华东" value="华东"></el-option>
                        <el-option label="华北" value="华北"></el-option>
                        <el-option label="华南" value="华南"></el-option>
                        <el-option label="华中" value="华中"></el-option>
                        <el-option label="西南" value="西南"></el-option>
                        <el-option label="西北" value="西北"></el-option>
                        <el-option label="东北" value="东北"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="区域描述">
                    <el-input v-model="currentLocation.Descr" type="textarea"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确认</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { LocationMaster } from '@/viewModel/system'
import { getLocationList, addLocation, updateLocation, deleteLocation } from '@/api/system/location'

export default defineComponent({
    Name: 'LocationManagement',
    setup() {
        const locationList = ref<LocationMaster[]>([])
        const dialogVisible = ref(false)
        const isEditMode = ref(false)
        const dialogTitle = ref('新增区域')
        const currentLocation = ref<LocationMaster>({
            Id: '',
            Name: '',
            Area: '',
            Descr: '',
            CreatedBy: '',
            CreatedAt: undefined
        })


        const fetchLocations = async () => {
            try {
                const res = await getLocationList()
                if (res.Code == 200 && res.Data != null) {
                    locationList.value = res.Data
                }

            } catch (error) {
                ElMessage.error('获取区域列表失败')
            }
        }
        const handleAddLocation = () => {
            isEditMode.value = false
            dialogTitle.value = '新增区域'
            currentLocation.value = {
                Id: '',
                Name: '',
                Area: '',
                Descr: '',
                CreatedBy: '',
                CreatedAt: undefined
            }
            dialogVisible.value = true
        }

        const handleEdit = (location: LocationMaster) => {
            isEditMode.value = true
            dialogTitle.value = '编辑区域'
            currentLocation.value = JSON.parse(JSON.stringify(location))
            dialogVisible.value = true
        }

        const handleDelete = (locationId: string) => {
            ElMessageBox.confirm('确定要删除该区域吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                try {
                    await deleteLocation(locationId)
                    ElMessage.success('删除成功')
                    fetchLocations()
                } catch (error) {
                    ElMessage.error('删除失败')
                }
            })
        }

        const submitForm = async () => {
            try {
                if (isEditMode.value) {
                    await updateLocation(currentLocation.value)
                    ElMessage.success('更新成功')
                } else {
                    await addLocation(currentLocation.value)
                    ElMessage.success('添加成功')
                }
                dialogVisible.value = false
                fetchLocations()
            } catch (error) {
                ElMessage.error('操作失败')
            }
        }

        onMounted(() => {
            fetchLocations()
        })

        return {
            locationList,
            dialogVisible,
            dialogTitle,
            currentLocation,
            handleAddLocation,
            handleEdit,
            handleDelete,
            submitForm
        }
    }
})
</script>

<style scoped>
.location-management {
    padding: 20px;
}
</style>